import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import store from './store'
//挂载vuex
Vue.prototype.$store = store
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
    ...App
})
import uView from '@/uni_modules/uview-ui'
import api from "@/common/api"
import {
  request,
  checkAuth,
} from './common/index';
Vue.use(uView)
Vue.prototype.$request = request;
Vue.prototype.$checkAuth = checkAuth;
Vue.prototype.$api = api;

Vue.prototype.$failToast = function (tip = '请求失败,请稍后再试~', icon = 'none') {
  uni.showToast({
    title: tip,
    icon
  })
}

// 全局加载KaTeX字体
function loadKatexFonts(retryCount = 0) {
  try {

    // 定义需要加载的字体列表
    const fonts = [
      { family: 'KaTeX_AMS', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_AMS-Regular.woff2' },
      { family: 'KaTeX_Caligraphic', weight: '700', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Caligraphic-Bold.woff2' },
      { family: 'KaTeX_Caligraphic', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Caligraphic-Regular.woff2' },
      { family: 'KaTeX_Fraktur', weight: '700', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Fraktur-Bold.woff2' },
      { family: 'KaTeX_Fraktur', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Fraktur-Regular.woff2' },
      { family: 'KaTeX_Main', weight: '700', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Main-Bold.woff2' },
      { family: 'KaTeX_Main', style: 'italic', weight: '700', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Main-BoldItalic.woff2' },
      { family: 'KaTeX_Main', style: 'italic', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Main-Italic.woff2' },
      { family: 'KaTeX_Main', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Main-Regular.woff2' },
      { family: 'KaTeX_Math', style: 'italic', weight: '700', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Math-BoldItalic.woff2' },
      { family: 'KaTeX_Math', style: 'italic', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Math-Italic.woff2' },
      { family: 'KaTeX_SansSerif', weight: '700', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_SansSerif-Bold.woff2' },
      { family: 'KaTeX_SansSerif', style: 'italic', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_SansSerif-Italic.woff2' },
      { family: 'KaTeX_SansSerif', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_SansSerif-Regular.woff2' },
      { family: 'KaTeX_Script', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Script-Regular.woff2' },
      { family: 'KaTeX_Size1', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Size1-Regular.woff2' },
      { family: 'KaTeX_Size2', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Size2-Regular.woff2' },
      { family: 'KaTeX_Size3', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Size3-Regular.woff2' },
      { family: 'KaTeX_Size4', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Size4-Regular.woff2' },
      { family: 'KaTeX_Typewriter', url: 'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/fonts/KaTeX_Typewriter-Regular.woff2' }
    ];

    // 加载每个字体
    let loadedCount = 0;
    const totalFonts = fonts.length;
    
    // 在小程序环境中加载字体
    if (typeof wx !== 'undefined' && wx.loadFontFace) {
      console.log('Loading KaTeX fonts...');
      fonts.forEach(font => {
        const descriptor = {
          family: font.family,
          global: true,
          source: `url("${font.url}")`
        };
        
        if (font.style) {
          descriptor.style = font.style;
        }
        
        if (font.weight) {
          descriptor.weight = font.weight;
        }

        wx.loadFontFace({
          ...descriptor,
          success: (res) => {
            console.log('Font loaded successfully:', font.family, res);
          },
          fail: (res) => {
            console.error('Font load failed:', font.family, res);
          }
        });
      });
    } else {
      console.log('wx.loadFontFace not available, skipping font loading');
    }
  } catch (error) {
    console.error('Error loading KaTeX fonts:', error);
  }
}

// 在应用启动时加载字体
try {
  loadKatexFonts();
} catch (error) {
  console.error('Failed to initialize KaTeX fonts:', error);
}

uni.getSystemInfo({
	success(e) {
		Vue.prototype.statusBarHeight = e.statusBarHeight;
		Vue.prototype.platform = e.platform;
	}
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif